﻿#if NETFX || NETCORE

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace Apewer.Surface
{

    /// <summary></summary>
    public class BlockTable : DataGridView
    {

        /// <summary></summary>
        public BlockTable()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true);
            UpdateStyles();

            this.Font = FormsUtility.DefaultFont;
            this.ColumnHeadersHeight = 32;
            this.RowHeadersVisible = false;
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.MultiSelect = false;
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            this.BackgroundColor = FormsUtility.White;
            this.GridColor = FormsUtility.GraceBorder;
            this.BorderStyle = BorderStyle.None;
            this.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;

            this.EnableHeadersVisualStyles = false;
            this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.AdvancedColumnHeadersBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
            this.AdvancedColumnHeadersBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
            this.AdvancedColumnHeadersBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
            this.AdvancedColumnHeadersBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

            this.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

            this.ReadOnly = true;
            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;
            this.AllowUserToOrderColumns = false;
            this.AllowUserToResizeColumns = true;
            this.AllowUserToResizeRows = false;

            this.RowsDefaultCellStyle.BackColor = Color.White;
            // this.RowsDefaultCellStyle.DataSourceNullValue ;
            this.RowsDefaultCellStyle.SelectionBackColor = FormsUtility.GraceWall;
            this.RowsDefaultCellStyle.SelectionForeColor = FormsUtility.Black;
            this.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.False;
        }

        /// <summary></summary>
        public void DisableSort()
        {
            for (int i = 0; i < this.Columns.Count; i++)
            {
                this.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
            }
        }

        /// <summary></summary>
        public void EnableSort()
        {
            for (int i = 0; i < this.Columns.Count; i++)
            {
                this.Columns[i].SortMode = DataGridViewColumnSortMode.Automatic;
            }
        }

        /// <summary></summary>
        public void Beautify()
        {
            this.ColumnHeadersHeight = 30;
            for (int i = 0; i < this.Columns.Count; i++)
            {
                this.Columns[i].HeaderCell.Style.BackColor = FormsUtility.GraceBorder;
                //this.Columns[i].HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            }
            for (int i = 0; i < this.Rows.Count; i++)
            {
                this.Rows[i].Height = 30;
            }
        }

        /// <summary></summary>
        public void Beautify(int rowIndex)
        {
            this.ColumnHeadersHeight = 30;
            if (rowIndex >= 0)
            {
                if (rowIndex < this.Rows.Count)
                {
                    this.Rows[rowIndex].Height = 30;
                }
            }
        }

        /// <summary>设置指定单元格的文本颜色。</summary>
        /// <param name="row">行。</param>
        /// <param name="column">列。</param>
        /// <param name="color">文本颜色。</param>
        public void SetCellColor(int row, string column, Color color)
        {
            try
            {
                Rows[row].Cells[column].Style.ForeColor = color;
                Rows[row].Cells[column].Style.SelectionForeColor = color;
            }
            catch { }
        }

        /// <summary>设置指定单元格的文本颜色为红色。</summary>
        public void SetCellRed(int row, string column) => SetCellColor(row, column, FormsUtility.Red);

        /// <summary>设置指定单元格的文本颜色为绿色。</summary>
        public void SetCellGreen(int row, string column) => SetCellColor(row, column, FormsUtility.Green);

        /// <summary>设置指定单元格的文本颜色为蓝色。</summary>
        public void SetCellBlue(int row, string column) => SetCellColor(row, column, FormsUtility.Blue);

        /// <summary>设置指定单元格的文本颜色为黑色。</summary>
        public void SetCellBlack(int row, string column) => SetCellColor(row, column, Color.Black);

    }
}

#endif
